package gameLevel.survivalLevel;

import gameBasics.GameModel;
import object.Iron;
import object.Wall;
import tank.*;
import java.util.Random;

public class SurvivalLevel {

    public SurvivalLevel(){
        MainTank mainTank = GameModel.getInstance().getMainTank();
        mainTank.setLivingState(true);
        initSurvivalLevel(mainTank);
    }

    private void initSurvivalLevel(MainTank mainTank) {
        //添加主坦克
        GameModel.getInstance().addGameObject(mainTank);

        //初始化地形
        initTerrainLayout();

        //初始化敌方坦克
        initEnemyTanks();
    }

    /**
     * 初始化敌方坦克
     */
    private void initEnemyTanks(){
        Random random = new Random();

        //初始化敌方坦克(先添加10辆)
        for(int i = 0; i < 10; i++){
            Tank enemyTank = new EnemyTank(TankType.values()[random.nextInt(1,5)],30 + i * 80, 210, Dir.DOWN);
            enemyTank.setMovingState(true);
        }

        //利用多线程生成敌方坦克
        new EnemyTankThread().start();
    }

    /**
     * 初始化地形布局
     */
    private void initTerrainLayout(){
        for(int i = 0; i < 6; i++){
            new Iron(150, 300 + i * 30);
            new Iron(760 - i * 30, 100);

            if(i >= 1){
                new Wall(360, 30 + (i - 1) * 30);
                new Wall(420, 30 + (i - 1) * 30);
            }

            if(i >= 2){
                new Iron(90, 30 + (i - 2) * 30);
                new Iron(180 + (i - 2) * 30, 300);
                new Wall(210 + (i - 2) * 30, 480);
                new Iron(500, 560 - (i - 2) * 30);
            }

            if(i >= 3){
                new Iron(300, 390 + (i - 3) * 30);
                new Iron(390, 30 + (i - 3) * 30);
                new Wall(530 + (i - 3) * 30, 410);
            }

            if(i >= 4){
                new Iron(150 + (i - 4) * 30, 480);
                new Iron(210 + (i - 4) * 30, 150);
                new Iron(210 + (i - 4) * 30, 180);
                new Iron(440 + (i - 4) * 30, 280);
                new Iron(440 + (i - 4) * 30, 310);
                new Wall(500, 440 - (i - 4) * 30);
                new Iron(620, 520 - (i - 4) * 30);
                new Iron(760 - (i - 4) * 30, 410);
            }
        }
    }

}
